package com.zqwh.yj.sdk.utils;

import android.hardware.Camera;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by eason.yang on 2017/9/28.
 */

public class CommonUtils {

    public static List<String> tagsToEpcs(Map<String, Integer> tags){
        return new ArrayList<>(tags.keySet());
    }

    public static String listToString(List<String> list){
        String result = "";
        if(list != null){
            for (int i = 0; i < list.size(); i++){
                if(i == 0){
                    result = list.get(i);
                }else {
                    result = result+","+list.get(i);
                }
            }
        }
        return result;
    }

    public static String listTagToString(List<String> list){
        String result = "";
        if(list != null){
            for (int i = 0; i < list.size(); i++){
                if(i == 0){
                    result = list.get(i).split("#")[0];
                }else {
                    result = result+","+list.get(i).split("#")[0];
                }
            }
        }
        return result;
    }

    public static List<String> stringToList(String str, String separatrix) {
        String[] arr = str.split(separatrix);
        List<String> list = Arrays.asList(arr);
        return list;
    }

    public static List<String> getSoldGoodsTagList(List<String> oldTags, List<String> newTags){
        List<String> reduceTagList = new ArrayList<>();    //减少的盘点数据
        if(oldTags!=null && newTags!=null){
            for(String oldTag : oldTags){
                if(!newTags.contains(oldTag)){
                    reduceTagList.add(oldTag);
                }
            }
        }
        return reduceTagList;
    }

    public static List<String> getShelveGoodsTagList(List<String> oldTags, List<String> newTags){
        ArrayList<String> increaseTagList = new ArrayList<>();  //增加的盘点数据
        if(oldTags!=null && newTags!=null){
            for(String newTag : newTags){
                if(!oldTags.contains(newTag)){
                    increaseTagList.add(newTag);
                }
            }
        }
        return increaseTagList;
    }

    /**
     * 判断摄像头是否可用
     * 主要针对6.0 之前的版本，现在主要是依靠try...catch... 报错信息，感觉不太好，
     * 以后有更好的方法的话可适当替换
     *
     * @return
     */
    public static boolean isCameraCanUse() {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
            // setParameters 是针对魅族MX5 做的。MX5 通过Camera.open() 拿到的Camera
            // 对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
        }
        return canUse;
    }
}
